#include "StoneBlocks.h"


StoneBlocks::StoneBlocks(GameState *gameState, int unitsWide, int unitsHigh):
				PhysicalObject(gameState), 
				m_unitsWide(unitsWide),
				m_unitsHigh(unitsHigh)
{
	addProperty("StoneBlocks");
	addProperty("ground");
	addProperty("block");
	setLayer(5);

	m_texture = gameState->getTextureManager().getTexture("stoneBlock.png");

	setSize(b2Vec2(unitsWide, unitsHigh));

	getBodyDef()->type = b2_kinematicBody;
	getBodyDef()->fixedRotation = true;
	b2FixtureDef *fixDef = new b2FixtureDef;
	b2PolygonShape *shape = new b2PolygonShape;
	shape->SetAsBox(m_size.x-0.1f, m_size.y-0.1f);
	fixDef->shape = shape;
	fixDef->density = 2.0f;
	fixDef->restitution = 0.0f;
	fixDef->friction = 0;
	addFixtureDef(fixDef);
	setRotation(0.0f);
}


StoneBlocks::~StoneBlocks()
{
}


void StoneBlocks::draw(){

	//drawFixtures();

	b2Vec2 pos = getPosition();

	

	glPushMatrix();
	{
		glTranslatef(pos.x, pos.y+1.0f, 0); 
		glScalef(2, 2, 1);

		sf::Texture::bind(m_texture); 
		for(int i = 0; i < m_unitsWide; i++){
			for(int j = 0; j < m_unitsHigh; j++){
				glPushMatrix();
				{
					glTranslatef( i-(m_unitsWide-1)*0.5f+((i+j)%2)*0.0f, j-(m_unitsHigh)*0.5f, 0);
					glBegin(GL_QUADS);
					{
						glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f, -0.2f);
						glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, -0.2f);
						glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f, -0.2f);
						glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.5f, 0.5f, -0.2f);
					}
					glEnd();
				}
				glPopMatrix();
			}
		}

		
	}
	glPopMatrix();

	sf::Texture::bind(NULL);
}